## Figures 1
rm(list = ls())
library(ggplot2)
library(ggridges)
library(dplyr)
library(viridis)
library(grid)
library(readr)
#########################################################################################

data_tek <- read.csv("Final_JPR/data/data-tek.csv")

data_tek <- data_tek %>%
       dplyr::select(gwgroupid, year, countries_gwid, groupname,
                     tek_groupid_arr, tek_count) %>%
       dplyr::filter(year > 1990)


groupdata <- data_tek

### Figure 1
g_hist <- groupdata %>% dplyr::select(gwgroupid, tek_count, year) %>% 
       dplyr::group_by(gwgroupid) %>% 
       dplyr::summarise(count = n(),
                        tek = sum(tek_count)) %>% 
       dplyr::mutate(tek_avg = round(tek / count, 0)) %>% 
       dplyr::group_by(tek_avg) %>% 
       dplyr::summarise(count = n()) %>% 
       dplyr::mutate(ptc = count/sum(count))  

p2 <-  ggplot(g_hist, aes(x= tek_avg, y= count)) + geom_bar(stat="identity",fill = "#FF6666") +
       scale_x_continuous(name = "Average TEK count", breaks = seq(0, 31, 1)) +
       # theme_grey() + 
       scale_y_continuous(expand = c(0.02, 0)) + #remove space between geom
       geom_text(data=g_hist, aes(label=count,
                                  #geom_text(data=g_hist, aes(label=paste0(round(ptc*100,1),"%"),
                                  y=count + 6), size=3) + 
       labs(#title = "Distribution of TEK count at group level",
              caption = paste("Total groups", sum(g_hist$count), sep = ": ")) +
       theme(axis.text = element_text(size=12,vjust= 1,hjust= 0.5), 
             text = element_text(size=12),
             axis.title=element_text(size=12)) + 
       theme_bw()

ggsave("Final_JPR/Figures/fig1.pdf", width = 6, height = 3.5, units='in', dpi=600)
ggsave("Final_JPR/Figures/fig1.jpg", width = 6, height = 3.5, units='in', dpi=600)


p2 <-  ggplot(g_hist, aes(x= tek_avg, y= count)) + geom_bar(stat="identity",fill = "black") +
       scale_x_continuous(name = "Average TEK count", breaks = seq(0, 31, 1)) +
       # theme_grey() + 
       scale_y_continuous(expand = c(0.02, 0)) + #remove space between geom
       geom_text(data=g_hist, aes(label=count,
       #geom_text(data=g_hist, aes(label=paste0(round(ptc*100,1),"%"),
                                  y=count + 6), size=3) + 
       labs(#title = "Distribution of TEK count at group level",
              caption = paste("Total groups", sum(g_hist$count), sep = ": ")) +
       theme(axis.text = element_text(size=12,vjust= 1,hjust= 0.5), 
             text = element_text(size=12),
             axis.title=element_text(size=12)) + 
       theme_bw()
ggsave("Final_JPR/Figures/black_figures/fig1.jpg", width = 6, height = 3.5, units='in', dpi=600)

